import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		partyMeetInfo: [],
		meetId: "",
		status: '',
		statusName: '',
		sign: '',
		signResult: '',
		summary: '',
		'isShowChecks': !1,

		'isShowCheck': !1,
		'isLeavePop': !1,
		'isShowMask': !1,
		userId: getApp().globalData.userId,
		huodongbaoming: true,
		yibaoming: false,

		leave: true,
		askedleave: false,
		flag: false, 	 //扫码是否禁用
    height:'',

	},

  onPageScroll: function (e) {
    this.setData({
      height: (e.scrollTop + 553),
    }) 
    console.log("height" + this.data.height);
  },

	onLoad(params) {
		var status = params.status;
		var meetId = params.meetId;
		var sign = params.sign;
		if (sign == 2) {		//已报名
			this.setData({
				huodongbaoming: false,
				yibaoming: true,
			})
		} else if (sign == 1) {	//未报名
			this.setData({
				huodongbaoming: true,
				yibaoming: false,
			})
		} else if (sign == 5) {	//已请假
			this.setData({
				askedleave: true,
				leave: false,
			})
		}
		this.setData({
			meetId: meetId,
			status: status,
			sign: sign,
		});
		//支部会议详情查询接口
		this.loadpartyMeetInfo();

	},

	//支部会议详情查询接口
	loadpartyMeetInfo: function() {
		var newUrl = getApp().globalData.dangjianUrl + "/party/partyMeetInfo";
		var params = {};
		params["meetId"] = this.data.meetId + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "支部会议详情查询接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var meet = respData.data.meet;
					var DateTime = getApp().formatToDate(meet.startDate, meet.endDate);
					meet.DateTime = DateTime;
					var statusName;
					console.log(meet)
					//开始15分钟前  扫码置灰
					var sysTime = new Date().getTime();
					var upstartDate = new Date(meet.startDate).getTime() - 15 * 60 * 1000;

					//开始15分钟后 扫码置灰
					var downstartDate = new Date(meet.startDate).getTime() + 15 * 60 * 1000;
					if (meet.status == 1) {
						if (sysTime < upstartDate) {
							meet.flag = true;
						} else if (sysTime >= upstartDate && sysTime <= new Date(meet.startDate).getTime()) {
							meet.flag = false;
						} else {
							meet.flag = false;
						}
					} else if (meet.status == 2) {
						if (sysTime >= downstartDate) {
							meet.flag = true;
						} else {
							meet.flag = false;
						}
					}
					this.setData({
						"partyMeetInfo": meet,
						"summary": meet.summary,
						"statusName": statusName,
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}

			}
		);
	},

	//支部会议请假接口
	loadpartyMeetSign: function() {
		var meetId = this.data.meetId;
		var newUrl = getApp().globalData.dangjianUrl + "/party/partyMeetSign";
		var params = {};
		params["meetId"] = this.data.meetId + "";
		params["token"] = "";
		params["status"] = "2";
		params["result"] = this.data.signResult + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "支部会议参加/签到/请假接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					this.formReset();
					this.setData({
						leave: !this.data.leave,
						askedleave: !this.data.askedleave,
						askedleaveOk: !this.data.askedleave,
						sign: 5,
					})
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2];
					var partyMeetList = prevPage.data.partyMeetList;
					for (var i in partyMeetList) {
						if (partyMeetList[i].meetId == meetId) {
							partyMeetList[i].sign = 5
						}
					}
					prevPage.loadpartyMeetList();
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	//扫码签到
	scanningCheck: function(qrMeetId) {
		var meetId = this.data.meetId;
		var newUrl = getApp().globalData.dangjianUrl + "/party/partyMeetSign";
		var params = {};
		params["meetId"] = qrMeetId + "";
		params["token"] = "";
		params["status"] = "1";
		params["result"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "支部会议参加/签到/请假接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2];
					var partyMeetList = prevPage.data.partyMeetList;
					for (var i in partyMeetList) {
						if (partyMeetList[i].meetId == meetId) {
							partyMeetList[i].sign = 4
						}
					}
					prevPage.loadpartyMeetList();
				} else {
				}
				wx.navigateTo({
					url: '../scanForMeeting/scanForMeeting?code=' + result +
						'&title=' + respData.data.result.description +
						'&meetId=' + qrMeetId +
						'&status=' + this.data.status +
						'&sign=' + this.data.sign,
				})
			}
		);
	},
	askedleaveOk() {
		this.setData({
			askedleaveOk: !this.data.askedleaveOk,
		})
	},


	ChecksIn() {
		let that = this;
		that.setData({
			'isShowChecks': !0
		})
	},
	checksInOk() {
		let that = this;
		that.setData({
			'isShowChecks': !1,
			huodongbaoming: !this.data.huodongbaoming,
			yibaoming: !this.data.yibaoming,
		})
	},

	CheckIn(event) {
		let that = this;
		that.setData({
			// 'isShowCheck': !0
		})
    wx.scanCode({
			type: 'qr',
			success: (res) => {
				//1.拿到扫码的结果
				var meetId = res.code;
				//调用接口签到
				this.scanningCheck(meetId.substring(meetId.indexOf('=') + 1, meetId.length));
				//根据签到是否成功定向到不同页面
			},
		});
	},
	checkInOk() {
		let that = this;
		that.setData({
			'isShowCheck': !1,
			'isShowMask': !1,
		})
	},
	leave() {
		let that = this;

		that.setData({
			'isLeavePop': !0,
			'isShowMask': !0
		})
	},
	cancel() {
		let that = this;

		that.setData({
			'isLeavePop': !1,
			'isShowMask': !1
		})
	},
	checkLeaveOk(e) {
		let that = this;
		that.setData({
			'isLeavePop': !1,
			'isShowMask': !1
		})

	},

	//请假提交
	formSubmit: function(e) {
		var signResult = e.detail.value.reason;
		if (signResult != undefined && signResult != "") {
			this.setData({
				signResult: signResult,
			});
			this.loadpartyMeetSign();
		} else {
			this.showPageMsg("tips", "请填写请假原因！");
		}

	},
	//请假取消
	formReset: function() {
		let that = this;
		that.setData({
			'isLeavePop': !1,
			'isShowMask': !1
		})
	},
	//会议纪要页面
	gomeetingMore: function(event) {
		var meetId = event.target.dataset.meetid;
		var pageUrl = '../meetingMore/meetingMore?meetId=' + meetId;
		wx.navigateTo({
			url: pageUrl
		})
	},

	onReachBottom() {
		// 页面被拉到底部
		this.showPageMsg("tips", "没有更多数据了!");
	},


	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			msgList: []
		});
		this.loadMsg();
		wx.stopPullDownRefresh();
	},
});
